using System;
using System.Collections.Generic;
using Activa.CoolStorage;
using CodeStory.Gallery.ObjectEntity;
using System.Web;
using CodeStory.Core.Services;
using System.IO;

namespace CodeStory.Gallery.ObjectBusiness
{
	public class GalleryPhotoBusiness
	{
		private GalleryPhotoBusiness() {

		}

		public static void DeletePhotosInFolder(Guid folderid) {
			//throw new Exception("The method or operation is not implemented.");
			CSList<GalleryPhotoObject> list = GalleryPhotoObject.List("FolderID=@FolderID","@FolderID",folderid);
			foreach (GalleryPhotoObject photo in list) {
				DeletePhotoObject(photo);
			}
		}

		private static void DeletePhotoObject(GalleryPhotoObject photo) {
			string str1 = HttpContext.Current.Request.PhysicalApplicationPath;
			string str3 = photo.PhotoPath;
			string path = FileUtilityService.Combine(str1,str3);
			if (File.Exists(path)) {
				try {
					File.Delete(path);
				} catch (UnauthorizedAccessException) {
					FileInfo f = new FileInfo(path);
					f.Attributes &= (~FileAttributes.ReadOnly);
					try {
						f.Delete();
					} catch {
					}
				} catch {
				}
			}
			photo.Delete();
		}

		public static IEnumerable<GalleryPhotoObject> ListPhotosInFolder(string folderid) {
			IEnumerable<GalleryPhotoObject> list;
			Guid id = Guid.Empty;
			try {
				id = new Guid(folderid);
			} catch {
			}
			if (id.Equals(Guid.Empty)) {
				list = new CSList<GalleryPhotoObject>();
			} else {
				list = GalleryPhotoObject.List("FolderID=@FolderID", "@FolderID", id);
			}
			return list;
		}
	}
}